package com.laomo.lib_music;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Handler;
import android.provider.Settings;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;

/**
 * 类说明：
 *
 * @author laomo
 * @date 2024/7/19 15:39
 */
public class MicInterface implements MicUtil.OnMicDataListener {
    private final MethodChannel mMicMethodChannel;
    private Handler mHandler = new Handler();
    public MicInterface(BinaryMessenger messenger) {
        MicUtil.getInstance().addOnMicDataListener(this);

        //文字绘制
        mMicMethodChannel = new MethodChannel(messenger, "mic_method_channel");
        mMicMethodChannel.setMethodCallHandler((call, result) -> {
            switch (call.method) {
                case "startRecord":
                    MicUtil.getInstance().startRecord();
                    break;
                case "stopRecord":
                    MicUtil.getInstance().stopRecord();
                    break;
            }
        });
    }

    private void invokeMethod(String methodName, Object arg) {
        mHandler.post(() -> {
            mMicMethodChannel.invokeMethod(methodName, arg);
        });
    }

    @Override
    public void onData(int[] data) {

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("data", data);
        invokeMethod("onMicData", dataMap);
    }
}
